package com.sgb.kjwl.viewmodel.self.logistics;

import android.util.Log;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.TransportOrderEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.TransportOrderActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
//承运列表
public class TransportOrderModel extends BaseViewModel {

/*
* 这个类
* */


    private TransportOrderActivity activity;
    private SelfRepertory repertory;
    int page=1;


    public TransportOrderModel(TransportOrderActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }


    public void getTransportOrderModel(boolean isMore) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        page=isMore?page+1:1;
        Map map = new HashMap();
        map.put("page",page);
        map.put("limit","10");

        APIManager.getInstance().getSelfOwnerAPI().myTransportOrderList(map).enqueue(new Callback<BaseEntity<ListResponse<List<TransportOrderEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TransportOrderEntity>>>> call, Response<BaseEntity<ListResponse<List<TransportOrderEntity>>>> response) {

                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.showTransportOrder(response.body().getData().getList(),isMore);
                } else {
                    assert response.body() != null;
                    MToast.showToast(activity, response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TransportOrderEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();

            }
        });
    }


    // 详情       货主订单详情 参数  goodsOrderDetails      货源详情参数   sourceDetails     ownerDetails
    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("goodsOrderDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    Log.e("madong_test","4====="+url);
                    showUrl(url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private void showUrl(String url) {
        WebViewX5HostActivity.start(activity, url);
    }


   public void confirm(String orderNo){
       DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
       APIManager.getInstance().getSelfOwnerAPI().orderConfirm(orderNo).enqueue(new Callback<BaseEntity>() {
           @Override
           public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
               DialogHelper.dismissProgressDialog();
               if (response.body() == null) {
                   return;
               }
               if (response.body().getState().equals("ok")) {

                   getTransportOrderModel(false);
               }else{
                   MToast.showToast(activity, response.body().getMsg());
               }
           }

           @Override
           public void onFailure(Call<BaseEntity> call, Throwable t) {
               DialogHelper.dismissProgressDialog();
           }
       });

   }
    public void delOrder(String orderNo){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getSelfOwnerAPI().orderDel(orderNo).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {

                    getTransportOrderModel(false);
                }else{
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
    public void cancel(String orderNo){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getSelfOwnerAPI().orderCancel(orderNo).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {

                    getTransportOrderModel(false);
                }else{
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
}
